#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Collections.Generic;
using System.Linq;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Data
{
	public static class UsageDAO
	{
		#region Get Usage Policy For System

		public static UsagePolicy GetUsagePolicyForSystem()
		{
			var policy = from p in Database.DC.UsagePolicies
			             where p.Username == "SYSTEM"
			             select p;

			return policy.Single();
		}

		#endregion Get Usage Policy For System

		#region Get Usage Policy For User

		public static UsagePolicy GetUsagePolicyForUser(string username)
		{
			var policy = from p in Database.DC.UsagePolicies
			             where p.Username == username
			             select p;

			return policy.Single();
		}

		#endregion Get Usage Policy For User

		public static List<string> GetAllUsagePolicyUsers()
		{
			var names = from p in Database.DC.UsagePolicies
			            select p.Username;

			return names.ToList();
		}

		public static void Delete(string name)
		{
			var policy = from p in Database.DC.UsagePolicies
			             where p.Username == name
			             select p;

			Database.DC.UsagePolicies.DeleteAllOnSubmit(policy);
			Database.DC.SubmitChanges();
		}

		public static void Insert(UsagePolicy policy)
		{
			Database.DC.UsagePolicies.InsertOnSubmit(policy);
			Database.DC.SubmitChanges();
		}

		public static void Update(UsagePolicy policy)
		{
			Database.DC.SubmitChanges();
		}

		public static int? GetAllowedPlayingTimeForUser(string username, DayOfWeek day)
		{
			var policy = from p in Database.DC.UsagePolicies
			             where p.Username == username
			             select p;

			UsagePolicy usagePolicy = policy.Single();

			if (day == DayOfWeek.Monday)
			{
				if (usagePolicy.Monday.HasValue)
					if (usagePolicy.MondayType.Value == (int) TimeType.Hours)
						return usagePolicy.Monday.Value*60;
					else
						return usagePolicy.Monday.Value;
			}
			else if (day == DayOfWeek.Tuesday)
			{
				if (usagePolicy.Tuesday.HasValue)
					if (usagePolicy.TuesdayType.Value == (int) TimeType.Hours)
						return usagePolicy.Tuesday*60;
					else
						return usagePolicy.Tuesday;
			}
			else if (day == DayOfWeek.Wednesday)
			{
				if (usagePolicy.Wednesday.HasValue)
					if (usagePolicy.WednesdayType.Value == (int) TimeType.Hours)
						return usagePolicy.Wednesday*60;
					else
						return usagePolicy.Wednesday;
			}
			else if (day == DayOfWeek.Thursday)
			{
				if (usagePolicy.Thursday.HasValue)
					if (usagePolicy.ThursdayType.Value == (int) TimeType.Hours)
						return usagePolicy.Thursday*60;
					else
						return usagePolicy.Thursday;
			}
			else if (day == DayOfWeek.Friday)
			{
				if (usagePolicy.Friday.HasValue)
					if (usagePolicy.FridayType.Value == (int) TimeType.Hours)
						return usagePolicy.Friday*60;
					else
						return usagePolicy.Friday;
			}
			else if (day == DayOfWeek.Saturday)
			{
				if (usagePolicy.Saturday.HasValue)
					if (usagePolicy.SaturdayType.Value == (int) TimeType.Hours)
						return usagePolicy.Saturday*60;
					else
						return usagePolicy.Saturday;
			}
			else if (day == DayOfWeek.Sunday)
			{
				if (usagePolicy.Sunday.HasValue)
					if (usagePolicy.SundayType.Value == (int) TimeType.Hours)
						return usagePolicy.Sunday*60;
					else
						return usagePolicy.Sunday;
			}

			return null;
		}
	}

	#region Old UsageDAO

	/*
	/// <summary>
	/// Usage Data Access Objects
	/// </summary>
	public class UsageDAO
	{
		#region Does Usage Policy Exist
		/// <summary>
		/// Checks to see if a UsagePolicy exists for a specified Id
		/// </summary>
		/// <param name="id">UsagePolicyId to check</param>
		/// <returns>True if the UsagePolicy exists, otherwise false</returns>
		public bool? DoesUsagePolicyExist(int? id)
		{
			if (id == null)
				throw new ArgumentNullException("id");

			if (GetUsagePolicy(id) == null)
				return false;
			else
				return true;
		}

		/// <summary>
		/// Checks to see if a UsagePolicy exists for a specified UserName
		/// </summary>
		/// <param name="userName">Windows UserName to check</param>
		/// <returns>True if UsagePolicy exists, otherwise False</returns>
		public bool? DoesUsagePolicyExist(string userName)
		{
			if (userName == null || userName.Length <= 0)
				throw new ArgumentNullException("userName");

			if (GetUsagePolicy(userName) == null)
				return false;
			else
				return true;
		}
		#endregion Does Usage Policy Exist

		#region Get Usage Policy
		/// <summary>
		/// Gets a UsagePolicy from the database by it's Id
		/// </summary>
		/// <param name="id">UsagePolicy.Id to retrieve for</param>
		/// <returns>Populated UsagePolicy object</returns>
		public UsagePolicy GetUsagePolicy(int? id)
		{
			if (id == null)
				throw new ArgumentNullException("id");

			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT Id, Username, StartDate, EndDate, Monday, MondayType, Tuesday, TuesdayType,
																						Wednesday, WednesdayType, Thursday, ThursdayType, Friday, FridayType, Saturday, 
																						SaturdayType, Sunday, SundayType
																					FROM UsagePolicy
																					WHERE (Id = @Id)");

			db.AddInParameter(dbCommand, "@Id", DbType.Int32, id);

			return FillUsagePolicyObject(db.ExecuteReader(dbCommand));
		}

		public UsagePolicy GetUsagePolicy(string userName)
		{
			if (userName == null)
				return null;

			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT Id, Username, StartDate, EndDate, Monday, MondayType, Tuesday, TuesdayType,
																						Wednesday, WednesdayType, Thursday, ThursdayType, Friday, FridayType,
																						Saturday, SaturdayType, Sunday, SundayType
																					FROM UsagePolicy
																					WHERE (Username = @Username)");

			db.AddInParameter(dbCommand, "@UserName", DbType.String, userName);

			return FillUsagePolicyObject(db.ExecuteReader(dbCommand));
		}
		#endregion Get Usage Policy

		#region Get Usage Policy For System
		/// <summary>
		/// Gets the Systems UsagePolicy
		/// </summary>
		/// <returns>Populated UsagePolicy for the System</returns>
		public UsagePolicy GetUsagePolicyForSystem()
		{
			return GetUsagePolicy("SYSTEM");
		}
		#endregion Get Usage Policy For Setting

		#region Get Usage Policy Users
		/// <summary>
		/// Gets a list of all users who have a UsagePolicy
		/// </summary>
		/// <returns>String array of user name with a UsagePolicy</returns>
		public string[] GetUsagePolicyUsers()
		{
			Database db;
			DbCommand dbCommand;
			ArrayList lst;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT Username
																						FROM UsagePolicy");

			lst = new ArrayList();
			IDataReader r = db.ExecuteReader(dbCommand);

			while (r.Read())
			{
				lst.Add(r[0].ToString());
			}

			return (string[])lst.ToArray(typeof(string));
		}
		#endregion Get Usage Policy Users

		#region Delete
		/// <summary>
		/// Delets a UsagePolicy from the database by UserName
		/// </summary>
		/// <param name="userName">User to delete the policy for</param>
		public void Delete(string userName)
		{
			if (userName == null || userName.Length <= 0)
				throw new ArgumentNullException("userName");

			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"DELETE
																						FROM UsagePolicy
																						WHERE (Username = @Username)");

			db.AddInParameter(dbCommand, "@UserName", DbType.String, userName);

			db.ExecuteNonQuery(dbCommand);
		}
		#endregion Delete

		#region Save
		/// <summary>
		/// Saves or Updates a UsagePolicy in the database
		/// </summary>
		/// <param name="usagePolicy">UsagePolicy to Save or Update</param>
		/// <returns>Populated UsagePolicy object</returns>
		public UsagePolicy Save(UsagePolicy usagePolicy)
		{
			if (usagePolicy == null)
				throw new ArgumentNullException("usagePolicy");

			if (usagePolicy.Id == null)
				usagePolicy = Insert(usagePolicy);
			else
				usagePolicy = Update(usagePolicy);

			return usagePolicy;
		}
		#endregion Save

		#region Insert
		/// <summary>
		/// Inserts a UsagePolicy into the database
		/// </summary>
		/// <param name="usagePolicy">UsagePolicy to Insert</param>
		/// <returns>Populated UsagePolicy Object</returns>
		private UsagePolicy Insert(UsagePolicy usagePolicy)
		{
			if (usagePolicy == null)
				throw new ArgumentNullException("usagePolicy");
			else if (usagePolicy.Id != null)
				throw new ArgumentException("UsagePolicy.Id must be null to perform an insert.");

			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"INSERT INTO [UsagePolicy]
																						 ([Username]
																						 ,[Monday]
																						 ,[MondayType]
																						 ,[Tuesday]
																						 ,[TuesdayType]
																						 ,[Wednesday]
																						 ,[WednesdayType]
																						 ,[Thursday]
																						 ,[ThursdayType]
																						 ,[Friday]
																						 ,[FridayType]
																						 ,[Saturday]
																						 ,[SaturdayType]
																						 ,[Sunday]
																						 ,[SundayType]
																						 ,[Hash]
																						 ,[TimeStamp])
																			 VALUES
																						 (@Username
																						 ,@Monday
																						 ,@MondayType
																						 ,@Tuesday
																						 ,@TuesdayType
																						 ,@Wednesday
																						 ,@WednesdayType
																						 ,@Thursday
																						 ,@ThursdayType
																						 ,@Friday
																						 ,@FridayType
																						 ,@Saturday
																						 ,@SaturdayType
																						 ,@Sunday
																						 ,@SundayType
																						 ,@Hash
																						 ,@Timestamp)
																		
																		SELECT Scope_Identity()");

			db.AddInParameter(dbCommand, "@UserName", DbType.String, usagePolicy.UserName);
			db.AddInParameter(dbCommand, "@Monday", DbType.Int32, usagePolicy.Monday);
			db.AddInParameter(dbCommand, "@MondayType", DbType.Int32, usagePolicy.MondayType);
			db.AddInParameter(dbCommand, "@Tuesday", DbType.Int32, usagePolicy.Tuesday);
			db.AddInParameter(dbCommand, "@TuesdayType", DbType.Int32, usagePolicy.TuesdayType);
			db.AddInParameter(dbCommand, "@Wednesday", DbType.Int32, usagePolicy.Wednesday);
			db.AddInParameter(dbCommand, "@WednesdayType", DbType.Int32, usagePolicy.WednesdayType);
			db.AddInParameter(dbCommand, "@Thursday", DbType.Int32, usagePolicy.Thursday);
			db.AddInParameter(dbCommand, "@ThursdayType", DbType.Int32, usagePolicy.ThursdayType);
			db.AddInParameter(dbCommand, "@Friday", DbType.Int32, usagePolicy.Friday);
			db.AddInParameter(dbCommand, "@FridayType", DbType.Int32, usagePolicy.FridayType);
			db.AddInParameter(dbCommand, "@Saturday", DbType.Int32, usagePolicy.Saturday);
			db.AddInParameter(dbCommand, "@SaturdayType", DbType.Int32, usagePolicy.SaturdayType);
			db.AddInParameter(dbCommand, "@Sunday", DbType.Int32, usagePolicy.Sunday);
			db.AddInParameter(dbCommand, "@SundayType", DbType.Int32, usagePolicy.SundayType);
			db.AddInParameter(dbCommand, "@Timestamp", DbType.DateTime, DateTime.Now);
			db.AddInParameter(dbCommand, "@Hash", DbType.String, usagePolicy.ComputeHash());

			usagePolicy.Id = int.Parse(db.ExecuteScalar(dbCommand).ToString());

			return usagePolicy;
		}
		#endregion Insert

		#region Update
		/// <summary>
		/// Updates a usage policy in the database
		/// </summary>
		/// <param name="usagePolicy">UsagePolicy object to update</param>
		/// <returns>Copy of persisted UsagePolicy object</returns>
		private UsagePolicy Update(UsagePolicy usagePolicy)
		{
			if (usagePolicy == null)
				throw new ArgumentNullException("usagePolicy");
			else if (usagePolicy.Id == null)
				throw new ArgumentException("UsagePolicy.Id must not be null to perform an update.");

			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"UPDATE [UsagePolicy]
																						SET [Username] = @Username
																								,[Monday] = @Monday
																								,[MondayType] = @MondayType
																								,[Tuesday] = @Tuesday
																								,[TuesdayType] = @TuesdayType
																								,[Wednesday] = @Wednesday
																								,[WednesdayType] = @WednesdayType
																								,[Thursday] = @Thursday
																								,[ThursdayType] = @ThursdayType
																								,[Friday] = @Friday
																								,[FridayType] = @FridayType
																								,[Saturday] = @Saturday
																								,[SaturdayType] = @SaturdayType
																								,[Sunday] = @Sunday
																								,[SundayType] = @SundayType
																								,[Timestamp] = @Timestamp
																								,[Hash] = @Hash
																						WHERE (Id = @Id)");

			db.AddInParameter(dbCommand, "@UserName", DbType.String, usagePolicy.UserName);
			db.AddInParameter(dbCommand, "@Monday", DbType.Int32, usagePolicy.Monday);
			db.AddInParameter(dbCommand, "@MondayType", DbType.Int32, usagePolicy.MondayType);
			db.AddInParameter(dbCommand, "@Tuesday", DbType.Int32, usagePolicy.Tuesday);
			db.AddInParameter(dbCommand, "@TuesdayType", DbType.Int32, usagePolicy.TuesdayType);
			db.AddInParameter(dbCommand, "@Wednesday", DbType.Int32, usagePolicy.Wednesday);
			db.AddInParameter(dbCommand, "@WednesdayType", DbType.Int32, usagePolicy.WednesdayType);
			db.AddInParameter(dbCommand, "@Thursday", DbType.Int32, usagePolicy.Thursday);
			db.AddInParameter(dbCommand, "@ThursdayType", DbType.Int32, usagePolicy.ThursdayType);
			db.AddInParameter(dbCommand, "@Friday", DbType.Int32, usagePolicy.Friday);
			db.AddInParameter(dbCommand, "@FridayType", DbType.Int32, usagePolicy.FridayType);
			db.AddInParameter(dbCommand, "@Saturday", DbType.Int32, usagePolicy.Saturday);
			db.AddInParameter(dbCommand, "@SaturdayType", DbType.Int32, usagePolicy.SaturdayType);
			db.AddInParameter(dbCommand, "@Sunday", DbType.Int32, usagePolicy.Sunday);
			db.AddInParameter(dbCommand, "@SundayType", DbType.Int32, usagePolicy.SundayType);
			db.AddInParameter(dbCommand, "@Timestamp", DbType.DateTime, DateTime.Now);
			db.AddInParameter(dbCommand, "@Hash", DbType.String, usagePolicy.ComputeHash());
			db.AddInParameter(dbCommand, "@Id", DbType.Int32, usagePolicy.Id.Value);

			db.ExecuteNonQuery(dbCommand);

			return usagePolicy;
		}
		#endregion Update

		#region Fill Usage Policy Object
		/// <summary>
		/// Populates a UsagePolicy object from a DataReader
		/// </summary>
		/// <param name="reader">DataReader to populate with</param>
		/// <returns>Populated UsagePolicy object</returns>
		private UsagePolicy FillUsagePolicyObject(IDataReader reader)
		{
			if (reader == null)
				throw new ArgumentNullException("reader");

			UsagePolicy policy = null;

			if (reader.Read())
			{
				policy = new UsagePolicy();

				if (!reader.IsDBNull(reader.GetOrdinal("Id")))
					policy.Id = reader.GetInt32(reader.GetOrdinal("Id"));

				if (!reader.IsDBNull(reader.GetOrdinal("UserName")))
					policy.UserName = reader.GetString(reader.GetOrdinal("UserName"));

				if (!reader.IsDBNull(reader.GetOrdinal("StartDate")))
					policy.StartDate = reader.GetDateTime(reader.GetOrdinal("StartDate"));

				if (!reader.IsDBNull(reader.GetOrdinal("EndDate")))
					policy.EndDate = reader.GetDateTime(reader.GetOrdinal("EndDate"));

				if (!reader.IsDBNull(reader.GetOrdinal("Monday")))
					policy.Monday = reader.GetInt32(reader.GetOrdinal("Monday"));

				if (!reader.IsDBNull(reader.GetOrdinal("MondayType")))
					policy.MondayType = (TimeType)reader.GetInt32(reader.GetOrdinal("MondayType"));

				if (!reader.IsDBNull(reader.GetOrdinal("Tuesday")))
					policy.Tuesday = reader.GetInt32(reader.GetOrdinal("Tuesday"));

				if (!reader.IsDBNull(reader.GetOrdinal("TuesdayType")))
					policy.TuesdayType = (TimeType)reader.GetInt32(reader.GetOrdinal("TuesdayType"));

				if (!reader.IsDBNull(reader.GetOrdinal("Wednesday")))
					policy.Wednesday = reader.GetInt32(reader.GetOrdinal("Wednesday"));

				if (!reader.IsDBNull(reader.GetOrdinal("WednesdayType")))
					policy.WednesdayType = (TimeType)reader.GetInt32(reader.GetOrdinal("WednesdayType"));

				if (!reader.IsDBNull(reader.GetOrdinal("Thursday")))
					policy.Thursday = reader.GetInt32(reader.GetOrdinal("Thursday"));

				if (!reader.IsDBNull(reader.GetOrdinal("ThursdayType")))
					policy.ThursdayType = (TimeType)reader.GetInt32(reader.GetOrdinal("ThursdayType"));

				if (!reader.IsDBNull(reader.GetOrdinal("Friday")))
					policy.Friday = reader.GetInt32(reader.GetOrdinal("Friday"));

				if (!reader.IsDBNull(reader.GetOrdinal("FridayType")))
					policy.FridayType = (TimeType)reader.GetInt32(reader.GetOrdinal("FridayType"));

				if (!reader.IsDBNull(reader.GetOrdinal("Saturday")))
					policy.Saturday = reader.GetInt32(reader.GetOrdinal("Saturday"));

				if (!reader.IsDBNull(reader.GetOrdinal("SaturdayType")))
					policy.SaturdayType = (TimeType)reader.GetInt32(reader.GetOrdinal("SaturdayType"));

				if (!reader.IsDBNull(reader.GetOrdinal("Sunday")))
					policy.Sunday = reader.GetInt32(reader.GetOrdinal("Sunday"));

				if (!reader.IsDBNull(reader.GetOrdinal("SundayType")))
					policy.SundayType = (TimeType)reader.GetInt32(reader.GetOrdinal("SundayType"));
			}

			return policy;
		}
		#endregion Fill Usage Policy Object
	}
    */

	#endregion Old UsageDAO
}